package com.jxb.jdbc;


import com.jxb.DO.User;

import java.sql.*;

public class Test {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Connection conn=null;

        PreparedStatement pstmt = null;

        try {
            //1：加载驱动
            Class.forName("com.mysql.jdbc.Driver");

            //2：创建连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root","123456");

            //sql语句
            String sql = "select * from user where id = ?";

            //获得sql执行者
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,1);

            //执行查询
            pstmt.execute();
            ResultSet rs = pstmt.getResultSet();

            rs.next();

            User user = new User();
            user.setId(rs.getInt("id"));
            user.setName(rs.getString("name"));
            System.out.println(user);
        }catch (Exception e) {

        }finally {
            if (conn != null) {
                conn.close();
            }
        }

    }

}
